package both.character;

import javax.swing.BoxLayout;
import javax.swing.Box;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.Random;
import java.util.ArrayList;

/**
 * Class to create a GUI to display information about a character.
 * @author Edd, Sam
 * TODO: Need to make it so you can choose characters.
 * TODO: Then figure out the best way to store weapons.
 */
public class CharacterDisplay extends JPanel
{
	private static final long serialVersionUID = 3195170444865344637L;
	private CharacterInformation information = new CharacterInformation();
	
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		new CharacterDisplay();
	}
	
	public CharacterDisplay()
	{
		createGUI();
	}
	
	private void createGUI()
	{
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT);
		
		int charID = 0;
		
		tabbedPane.addTab("Dice", null, makeDicePanel());
		
		JComponent panel2 = makeInfoPanel(information.getCharName(charID), information.getPlayerName(charID),
					information.getCharClass(charID), information.getLevel(charID), information.getLevel(charID),
					information.getRace(charID), information.getSize(charID), information.getGender(charID),
					information.getAlignment(charID), information.getReligion(charID), information.getHeight(charID),
					information.getWeight(charID), information.getLooks(charID));
		tabbedPane.addTab("Info", null, panel2);
		
		tabbedPane.addTab("Abilities", null, makeAbilityPanel(12, 12, 14 ,17, 15, 14));
		
		tabbedPane.addTab("Combat", null, makeCombatPanel());

		//tabbedPane.addTab("Saving", null, makeSavingPanel());
		
		//tabbedPane.addTab("Armour", null, makeArmourPanel());
		
		this.add(tabbedPane, BorderLayout.CENTER);
		
		//TODO: Remove when finished.
		JFrame frame = new JFrame("DnD Character Display");
	    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(this);
	    frame.pack();
		frame.setVisible(true);
	}
	
	private JComponent makeDicePanel()
	{
		final JTextField d4Qty = new JTextField("1");
		final JTextField d6Qty = new JTextField("1");
		final JTextField d8Qty = new JTextField("1");
		final JTextField d10Qty = new JTextField("1");
		final JTextField d100Qty = new JTextField("1");
		final JTextField d12Qty = new JTextField("1");
		final JTextField d20Qty = new JTextField("1");
		final JTextField dXTyp = new JTextField("1");
		final JTextField dXQty = new JTextField("1");
		final JTextField d4Result = new JTextField();
		final JTextField d6Result = new JTextField();
		final JTextField d8Result = new JTextField();
		final JTextField d10Result = new JTextField();
		final JTextField d100Result = new JTextField();
		final JTextField d12Result = new JTextField();
		final JTextField d20Result = new JTextField();
		final JTextField dXResult = new JTextField();
		
		JButton d4Roll = new JButton("Roll");
		d4Roll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                d4Result.setText("" + rollDie(4, Integer.parseInt(d4Qty.getText())));
            }
        });
		JButton d6Roll = new JButton("Roll");
		d6Roll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                d6Result.setText("" + rollDie(6, Integer.parseInt(d6Qty.getText())));
            }
        });
		JButton d8Roll = new JButton("Roll");
		d8Roll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                d8Result.setText("" + rollDie(8, Integer.parseInt(d8Qty.getText())));
            }
        });
		JButton d10Roll = new JButton("Roll");
		d10Roll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                d10Result.setText("" + rollDie(10, Integer.parseInt(d10Qty.getText())));
            }
        });
		JButton d100Roll = new JButton("Roll");
		d100Roll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                d100Result.setText("" + rollDie(100, Integer.parseInt(d100Qty.getText())));
            }
        });
		JButton d12Roll = new JButton("Roll");
		d12Roll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                d12Result.setText("" + rollDie(12, Integer.parseInt(d12Qty.getText())));
            }
        });
		JButton d20Roll = new JButton("Roll");
		d20Roll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                d20Result.setText("" + rollDie(20, Integer.parseInt(d20Qty.getText())));
            }
        });
		JButton dXRoll = new JButton("Roll");
		dXRoll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
            {
                dXResult.setText("" + rollDie(Integer.parseInt(dXTyp.getText()), Integer.parseInt(dXQty.getText())));
            }
        });
		
		
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(9, 4));
		
		panel.add(new JLabel("Die"));
		panel.add(new JLabel("Qty"));
		panel.add(new JLabel(""));
		panel.add(new JLabel("Result"));
		
		panel.add(new JLabel("D4"));
		panel.add(d4Qty);
		panel.add(d4Roll);
		panel.add(d4Result);
		
		panel.add(new JLabel("D6"));
		panel.add(d6Qty);
		panel.add(d6Roll);
		panel.add(d6Result);

		
		panel.add(new JLabel("D8"));
		panel.add(d8Qty);
		panel.add(d8Roll);
		panel.add(d8Result);

		
		panel.add(new JLabel("D10"));
		panel.add(d10Qty);
		panel.add(d10Roll);
		panel.add(d10Result);

		
		panel.add(new JLabel("D100"));
		panel.add(d100Qty);
		panel.add(d100Roll);
		panel.add(d100Result);

		
		panel.add(new JLabel("D12"));
		panel.add(d12Qty);
		panel.add(d12Roll);
		panel.add(d12Result);

		
		panel.add(new JLabel("D20"));
		panel.add(d20Qty);
		panel.add(d20Roll);
		panel.add(d20Result);

		
		panel.add(dXTyp);
		panel.add(dXQty);
		panel.add(dXRoll);
		panel.add(dXResult);
		
		return panel;
		
	}
	
	private int rollDie(int type, int qty)
	{
		int result = 0;
		
		ArrayList<Random> rands = new ArrayList<Random>();
		
		if (qty > 0) {
			for(int i = 0; i < qty; i++)
			{
				rands.add(new Random());
				result += (rands.get(i).nextInt(type) + 1);
			}
		}
		return result;
	}
	
	private JComponent makeInfoPanel(String cName, String pName,
							String cClass, int cLVL, int cECL, String cRace,
							String cSize, String cGender, String cAlignment,
							String cReligion, String cHeight, int cWeight,
							String cLooks)
	{
		JPanel panel = new JPanel();
		
		JLabel charNameLabel = new JLabel("Character Name ");
		charNameLabel.setHorizontalAlignment(JLabel.LEFT);
		JTextField charNameField = new JTextField(cName);
		charNameField.setHorizontalAlignment(JTextField.LEFT);
		JPanel character = new JPanel();
		BoxLayout charLayout = new BoxLayout(character, BoxLayout.LINE_AXIS);
		character.setLayout(charLayout);
		character.add(charNameLabel);
		character.add(charNameField);
		character.add(Box.createHorizontalGlue());
		
		JLabel playerNameLabel = new JLabel("Player Name ");
		playerNameLabel.setHorizontalAlignment(JLabel.LEFT);
		JTextField playerNameField = new JTextField(pName);
		playerNameField.setHorizontalAlignment(JTextField.LEFT);
        JPanel player = new JPanel();
        BoxLayout playerLayout = new BoxLayout(player, BoxLayout.LINE_AXIS);
        player.setLayout(playerLayout);
		player.add(playerNameLabel);
		player.add(playerNameField);
		player.add(Box.createHorizontalGlue());
		
		JLabel classLabel = new JLabel("Class ");
        classLabel.setHorizontalAlignment(JLabel.LEFT);
        JTextField classField = new JTextField(cClass);
        classField.setHorizontalAlignment(JTextField.LEFT);
        JPanel classPanel = new JPanel();
        BoxLayout classLayout = new BoxLayout(classPanel, BoxLayout.LINE_AXIS);
        classPanel.setLayout(classLayout);
        classPanel.add(classLabel);
        classPanel.add(classField);
        classPanel.add(Box.createHorizontalGlue());
        
        JLabel levelLabel = new JLabel("Level ");
        levelLabel.setHorizontalAlignment(JLabel.LEFT);
        JTextField levelField = new JTextField("" + cLVL);
        levelField.setHorizontalAlignment(JTextField.LEFT);
        JPanel level = new JPanel();
        BoxLayout levelLayout = new BoxLayout(level, BoxLayout.LINE_AXIS);
        level.setLayout(levelLayout);
        level.add(levelLabel);
        level.add(levelField);
        level.add(Box.createHorizontalGlue());
        
        JLabel eclLabel = new JLabel("ECL    ");
        eclLabel.setHorizontalAlignment(JLabel.LEFT);
        JLabel eclField = new JLabel("" + cECL);
        eclField.setHorizontalAlignment(JLabel.LEFT);
        JPanel ecl = new JPanel();
        BoxLayout eclLayout = new BoxLayout(ecl, BoxLayout.LINE_AXIS);
        ecl.setLayout(eclLayout);
        ecl.add(eclLabel);
        ecl.add(eclField);
        ecl.add(Box.createHorizontalGlue());
        
        JLabel raceLabel = new JLabel("Race ");
        raceLabel.setHorizontalAlignment(JLabel.LEFT);
        JTextField raceField = new JTextField(cRace);
        raceField.setHorizontalAlignment(JTextField.LEFT);
        JPanel race = new JPanel();
        BoxLayout raceLayout = new BoxLayout(race, BoxLayout.LINE_AXIS);
        race.setLayout(raceLayout);
        race.add(raceLabel);
        race.add(raceField);
        race.add(Box.createHorizontalGlue());
        
        JLabel sizeLabel = new JLabel("Size ");
        sizeLabel.setHorizontalAlignment(JLabel.LEFT);
        JTextField sizeField = new JTextField(cSize);
        sizeField.setHorizontalAlignment(JTextField.LEFT);
        JPanel size = new JPanel();
        BoxLayout sizeLayout = new BoxLayout(size, BoxLayout.LINE_AXIS);
        size.setLayout(sizeLayout);
        size.add(sizeLabel);
        size.add(sizeField);
        size.add(Box.createHorizontalGlue());
        
        JLabel genderLabel = new JLabel("Gender ");
        genderLabel.setHorizontalAlignment(JLabel.LEFT);
        JTextField genderField = new JTextField(cGender);
        genderField.setHorizontalAlignment(JTextField.LEFT);
        JPanel gender = new JPanel();
        BoxLayout genderLayout = new BoxLayout(gender, BoxLayout.LINE_AXIS);
        gender.setLayout(genderLayout);
        gender.add(genderLabel);
        gender.add(genderField);
        gender.add(Box.createHorizontalGlue());
        
        JLabel alignmentLabel = new JLabel("Alignment ");
        alignmentLabel.setHorizontalAlignment(JLabel.LEFT);
        JTextField alignmentField = new JTextField(cAlignment);
        alignmentField.setHorizontalAlignment(JTextField.LEFT);
        JPanel alignment = new JPanel();
        BoxLayout alignmentLayout = new BoxLayout(alignment, BoxLayout.LINE_AXIS);
        alignment.setLayout(alignmentLayout);
        alignment.add(alignmentLabel);
        alignment.add(alignmentField);
        alignment.add(Box.createHorizontalGlue());
        
        JLabel religionLabel = new JLabel("Religion/Deity ");
        religionLabel.setHorizontalAlignment(JLabel.LEFT);
        JTextField religionField = new JTextField(cReligion);
        religionField.setHorizontalAlignment(JTextField.LEFT);
        JPanel religion = new JPanel();
        BoxLayout religionLayout = new BoxLayout(religion, BoxLayout.LINE_AXIS);
        religion.setLayout(religionLayout);
        religion.add(religionLabel);
        religion.add(religionField);
        religion.add(Box.createHorizontalGlue());
        
        JLabel heightLabel = new JLabel("Height ");
        heightLabel.setHorizontalAlignment(JLabel.LEFT);
        JTextField heightField = new JTextField(cHeight);
        heightField.setHorizontalAlignment(JTextField.LEFT);
        JPanel height = new JPanel();
        BoxLayout heightLayout = new BoxLayout(height, BoxLayout.LINE_AXIS);
        height.setLayout(heightLayout);
        height.add(heightLabel);
        height.add(heightField);
        height.add(Box.createHorizontalGlue());
        
        JLabel weightLabel = new JLabel("Weight ");
        weightLabel.setHorizontalAlignment(JLabel.LEFT);
        JTextField weightField = new JTextField("" + cWeight);
        weightField.setHorizontalAlignment(JTextField.LEFT);
        JPanel weight = new JPanel();
        BoxLayout weightLayout = new BoxLayout(weight, BoxLayout.LINE_AXIS);
        weight.setLayout(weightLayout);
        weight.add(weightLabel);
        weight.add(weightField);
        weight.add(Box.createHorizontalGlue());
        
        JLabel looksLabel = new JLabel("Looks ");
        looksLabel.setHorizontalAlignment(JLabel.LEFT);
        JTextField looksField = new JTextField(cLooks);
        looksField.setHorizontalAlignment(JTextField.LEFT);
        JPanel looks = new JPanel();
        BoxLayout looksLayout = new BoxLayout(looks, BoxLayout.LINE_AXIS);
        looks.setLayout(looksLayout);
        looks.add(looksLabel);
        looks.add(looksField);
        looks.add(Box.createHorizontalGlue());
        
        GroupLayout layout = new GroupLayout(panel);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        panel.setLayout(layout);
        layout.setHorizontalGroup(layout.createSequentialGroup() 
        		.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        		           .addComponent(character)
        		           .addGroup(layout.createSequentialGroup()
        		        		   .addComponent(classPanel)
        		        		   .addComponent(level))
        		           .addGroup(layout.createSequentialGroup()
        		        		   .addComponent(alignment)
        		        		   .addComponent(religion)))
        		.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
        				.addComponent(player)
        		.addGroup(layout.createSequentialGroup()
    		           	.addComponent(ecl)
    		           	.addComponent(race)
    		           	.addComponent(size)
    		           	.addComponent(gender))
        		.addGroup(layout.createSequentialGroup()
    		           	.addComponent(height)
    		           	.addComponent(weight)
    		           	.addComponent(looks))
        		));
        layout.setVerticalGroup(layout.createSequentialGroup()
             .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
             		 .addComponent(character)
               		 .addComponent(player))
             .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
            		 .addComponent(classPanel)
                     .addComponent(level)
                   	 .addComponent(ecl)
                   	 .addComponent(race)
                   	 .addComponent(size)
        	   		 .addComponent(gender))
        	 .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                   	 .addComponent(alignment)
                   	 .addComponent(religion)
                   	 .addComponent(height)
                   	 .addComponent(weight)
                   	 .addComponent(looks))
        	);
        
		return panel;
	}
	
	private JComponent makeAbilityPanel(int strength, int dexterity,
				int constitution, int intelligence, int wisdom, int charisma)
	{
		int str = strength;
		int strEnh = 0;
		int strMisc = 0;
		int strPen = 0;
		int strTotal = str + strEnh + strMisc - strPen;
		int strMod = ((strTotal - 10) / 2);
		
		int dex = dexterity;
		int dexEnh = 0;
		int dexMisc = 0;
		int dexPen = 0;
		int dexTotal = dex + dexEnh + dexMisc - dexPen;
		int dexMod = ((dexTotal - 10) / 2);

		int con = constitution;
		int conEnh = 0;
		int conMisc = 0;
		int conPen = 0;
		int conTotal = con + conEnh + conMisc - conPen;
		int conMod = ((conTotal - 10) / 2);

		int intel = intelligence;
		int intelEnh = 0;
		int intelMisc = 0;
		int intelPen = 0;
		int intelTotal = intel + intelEnh + intelMisc - intelPen;
		int intelMod = ((intelTotal - 10) / 2);

		int wis = wisdom;
		int wisEnh = 0;
		int wisMisc = 0;
		int wisPen = 0;
		int wisTotal = wis + wisEnh + wisMisc - wisPen;
		int wisMod = ((wisTotal - 10) / 2);

		int cha = charisma;
		int chaEnh = 0;
		int chaMisc = 0;
		int chaPen = 0;
		int chaTotal = cha + chaEnh + chaMisc - chaPen;
		int chaMod = ((chaTotal - 10) / 2);
		
        JPanel panel = new JPanel();
        
        panel.setLayout(new GridLayout(7, 7));
        
        panel.add(new JLabel("Stat"));
        panel.add(new JLabel("Total"));
        panel.add(new JLabel("Base"));
        panel.add(new JLabel("Enchancement"));
        panel.add(new JLabel("Misc"));
        panel.add(new JLabel("Penalties"));
        panel.add(new JLabel("Modifier"));
        panel.add(new JLabel("STR"));
        
        panel.add(new JLabel("" + strTotal));
        panel.add(new JTextField("" + str));
        panel.add(new JTextField("" + strEnh));
        panel.add(new JTextField("" + strMisc));
        panel.add(new JTextField("" + strPen));
        panel.add(new JLabel("" + strMod));
        
        panel.add(new JLabel("DEX"));
        panel.add(new JLabel("" + dexTotal));
        panel.add(new JTextField("" + dex));
        panel.add(new JTextField("" + dexEnh));
        panel.add(new JTextField("" + dexMisc));
        panel.add(new JTextField("" + dexPen));
        panel.add(new JLabel("" + dexMod));
        
        panel.add(new JLabel("CON"));
        panel.add(new JLabel("" + conTotal));
        panel.add(new JTextField("" + con));
        panel.add(new JTextField("" + conEnh));
        panel.add(new JTextField("" + conMisc));
        panel.add(new JTextField("" + conPen));
        panel.add(new JLabel("" + conMod));
        
        panel.add(new JLabel("INT"));
        panel.add(new JLabel("" + intelTotal));
        panel.add(new JTextField("" + intel));
        panel.add(new JTextField("" + intelEnh));
        panel.add(new JTextField("" + intelMisc));
        panel.add(new JTextField("" + intelPen));
        panel.add(new JLabel("" + intelMod));
        
        panel.add(new JLabel("WIS"));
        panel.add(new JLabel("" + wisTotal));
        panel.add(new JTextField("" + wis));
        panel.add(new JTextField("" + wisEnh));
        panel.add(new JTextField("" + wisMisc));
        panel.add(new JTextField("" + wisPen));
        panel.add(new JLabel("" + wisMod));
        
        panel.add(new JLabel("CHA"));
        panel.add(new JLabel("" + chaTotal));
        panel.add(new JTextField("" + cha));
        panel.add(new JTextField("" + chaEnh));
        panel.add(new JTextField("" + chaMisc));
        panel.add(new JTextField("" + chaPen));
        panel.add(new JLabel("" + chaMod));
                
        return panel;
    }
	
	private JComponent makeCombatPanel()
	{
		JPanel panel = new JPanel();
		
		
		
		return panel;
	}
	
	
}
